//
//  DSZGZViewController.m
//  7.13苏宁框架
//
//  Created by app15 on 15/7/19.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "DSZGZViewController.h"
#import "DSZGZHeaderView.h"
#import "DSZGZTableViewCell.h"
#import "DSZTJViewController.h"
#import "DSZTJShowModel.h"
#import "DSZTJSuperModel.h"
#import "DSZTJGroupModel.h"
#import "MJExtension.h"
#import "UIImageView+WebCache.h"
#import "DSZGZMoreViewController.h"
#import "DSZMainViewController.h"
#import "DSZShowViewController.h"
#import "DSZGZXQViewController.h"
NSInteger gzgroupNum;
@interface DSZGZViewController ()<UITableViewDataSource,UITableViewDelegate,DSZGZHeaderViewDelegate,CareClickDelegate>
{
    NSInteger gzgroupNumone;
    UITableView *tableview;
}
@property(nonatomic,retain)NSArray *superModel;
@property(nonatomic,retain)NSArray *groupModel;
@end

@implementation DSZGZViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self superModel];
    
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    _care = [[defaults objectForKey:@"care"] integerValue];
    

    tableview=[[UITableView alloc]init];
    tableview.frame=CGRectMake(0, 0, 320, 480);
    DSZGZHeaderView *view=[[DSZGZHeaderView alloc]initWithFrame:CGRectMake(0, 0, 320, 100)];
    tableview.tableHeaderView=view;
    [self.view addSubview:tableview];
    tableview.delegate=self;
    tableview.dataSource=self;
    view.delegate=self;
    // Do any additional setup after loading the view.
}

-(void)moreView{
    DSZGZMoreViewController *more=[[DSZGZMoreViewController alloc]init];
    [self.navigationController pushViewController:more animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark  CareClickDelegate代理方法


-(void)clickCareBtn1:(UIButton *)btn model:(DSZTJGroupModel *)model{
    
    if (btn.tag == 1) {
        model.iscare = NO;
        _care--;
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setValue:@(_care) forKey:@"care"];
        btn.layer.borderColor = [UIColor orangeColor].CGColor;
        btn.selected=NO;
        btn.tag = 0;
    }else{
         model.iscare = YES;
        _care++;
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setValue:@(_care) forKey:@"care"];
        btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        btn.selected=YES;
        btn.tag = 1;
        
    }
    
    
}

//plist文件的数组
-(NSArray*)superModel{
    //plist文件的数组
    NSArray *array=[NSArray array];
    array=[DSZTJSuperModel modelArrayWithFilename:@"Show.plist"];
    
    //group组的数组
    NSMutableArray *arraymodel=[NSMutableArray array];
    for (DSZTJSuperModel *model in array) {
        NSDictionary *dic=[model keyValues];
        NSArray *groupArray=dic[@"group"];
        gzgroupNumone=gzgroupNumone+groupArray.count;
        
        NSArray *groupmodel=[DSZTJGroupModel modelArrayWithDictArray:groupArray];
        
        for (DSZTJGroupModel *model in groupmodel) {
          //  NSLog(@"%@",model);
            [arraymodel addObject:model];
        }
        
    }
    //调用方法获取group数组
    [self groupModel:arraymodel];
    
    return _superModel=array;
}

//返回group数组
-(NSArray*)groupModel:(NSArray*)arraymodel{
    return _groupModel=arraymodel;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return gzgroupNumone;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *IDS=@"cell";
    DSZGZTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:IDS];
    cell.delegate=self;
    DSZTJGroupModel *model= _groupModel[indexPath.row];
    if (cell==nil) {
        cell=[[DSZGZTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:IDS];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    [cell.groupimageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.image]]];
    NSInteger i=model.name.length;
    cell.titleLabel.frame=CGRectMake(cell.titleLabel.frame.origin.x, cell.titleLabel.frame.origin.y, i*15, cell.titleLabel.frame.size.height);
    cell.vipImage.frame=CGRectMake(cell.titleLabel.frame.origin.x+i*15+10, cell.vipImage.frame.origin.y, 12, 12);
    cell.tjmodel = model;
    cell.titleLabel.text=model.name;
    cell.intorLabel.text=model.intor;
    
    cell.delegate=self;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DSZGZXQViewController *view=[[DSZGZXQViewController alloc]init];
    DSZTJGroupModel *model=_groupModel[indexPath.row];
    [view XqGroupModel:model];
    [self.navigationController pushViewController:view animated:NO];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
//返回首页
- (IBAction)returnMain:(UIBarButtonItem *)sender {
    
    UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    DSZMainViewController *vc=[story instantiateViewControllerWithIdentifier:@"main"];
    [self presentViewController:vc animated:NO completion:nil];
}

@end
